package com.sduept.nwld.dataserver.controller.config;

import com.sduept.bigdata.fault.common.ParamDesc;
import com.sduept.bigdata.fault.entity.RequestUnfinishedTask;
import com.sduept.bigdata.fault.manager.RequestUnfinishedTaskManager;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.core.ParamManager;
import com.sduept.core.entity.SystemParameter;
import com.sduept.utils.DateUtil;
import com.sduept.utils.WebRequestUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection.Method;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortMeta;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.*;

@Named
@ViewScoped
public class DataSourceWarnController extends AbstractController {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1965407108262158194L;
	private static final String MSG_SUCCESS = "success";
	private static final String MSG_ERROR = "error";

	@Autowired
	private RequestUnfinishedTaskManager manager;
	@Autowired
	private ParamManager cache;

	private List<RequestUnfinishedTask> tasks = new ArrayList<RequestUnfinishedTask>();
	private LazyDataModel<RequestUnfinishedTask> model;
	private RequestUnfinishedTask selected;
	private Date queryDate1;
	private Date queryDate2;
	private int sort;// 排序所用
	private int status;
	private int recordSize; // 页面显示总计

	@PostConstruct
	public void init() {
		initTable();
	}

	public void initTable() {
		final String start;
		final String end;
		final Integer warnstatus;
		if (null != queryDate1) {
			start = DateUtil.getStringDate(queryDate1, DateUtil.pattern_ymd) + " 00:00:00";
		} else {
			start = null;
		}
		if (null != queryDate2) {
			end = DateUtil.getStringDate(queryDate2, DateUtil.pattern_ymd) + " 23:59:59";
		} else {
			end = null;
		}
		if (null != queryDate1 && null != queryDate2 && queryDate1.getTime() > queryDate2.getTime()) {
			addErrorMessage("开始时间不能大于结束时间！");
			return;
		}
		if (status == 2) {
			warnstatus = null;
		} else {
			warnstatus = status;
		}
		model = new LazyDataModel<RequestUnfinishedTask>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 6790001481484327585L;

			@Override
			public List<RequestUnfinishedTask> load(int first, int pageSize, String sortField, SortOrder sortOrder,
					Map<String, Object> filters) {
				sort = first;
				tasks = manager.listRequestUnfinishedTasksWithRange(warnstatus, start, end,
						new int[] { first, first + pageSize });
				return tasks;
			}

			@Override
			public List<RequestUnfinishedTask> load(int first, int pageSize, List<SortMeta> multiSortMeta,
					Map<String, Object> filters) {
				sort = first;
				tasks = manager.listRequestUnfinishedTasksWithRange(warnstatus, start, end,
						new int[] { first, first + pageSize });
				return tasks;
			}

			@Override
			public RequestUnfinishedTask getRowData(String rowKey) {
				for (RequestUnfinishedTask t : tasks) {
					if (t.getId().equals(rowKey)) {
						return t;
					}
				}
				return null;
			}

			@Override
			public Object getRowKey(RequestUnfinishedTask object) {
				for (RequestUnfinishedTask t : tasks) {
					if (t.getId().equals(object.getId())) {
						return t;
					}
				}
				return null;
			}
		};
		recordSize = manager.countRequestUnfinishedTasks(warnstatus, start, end);
		model.setRowCount(recordSize);
	}

	public String getDataType(int type) {
		if (0 == type) {
			return "数据为空";
		} else if (1 == type) {
			return "网络异常";
		}
		return "";
	}

	public String getStaus(int status) {
		if (0 == status) {
			return "告警中";
		} else if (1 == status) {
			return "已恢复";
		}
		return "";
	}

	public int getIndex(RequestUnfinishedTask task) {
		return tasks.indexOf(task) + sort + 1;
	}

	public void regetData(String id) {
		// 利用web 访问OSB数据库
		String osbUrl = getOSBUrl();
//		osbUrl="127.0.0.1";
//		id="5a937ae9a7ca533244d8e596";
		if (StringUtils.isEmpty(osbUrl)) {
			return;
		}
		try {
			String result = WebRequestUtil.getRequest("http://" + osbUrl + "/rest/dataRegetService/regetData/" + id,
					Method.GET, new HashMap<String, String>());
			if (MSG_SUCCESS.equals(result)) {
				addSuccessMessage("数据重新获取已执行，请耐心等待");
			} else {
				addErrorMessage("数据重新获取失败，请联系管理员");
			}
		} catch (Exception e) {
			e.printStackTrace();
			addErrorMessage("数据重新获取失败，请联系管理员");
		}
	}

	private String getOSBUrl() {
		String result = null;
		SystemParameter parameter = cache.get(ParamDesc.OSB_IP);
		if (null != parameter && StringUtils.isNotEmpty(parameter.getValue())) {
			result = parameter.getValue();
		}
		return result;
	}

	public List<RequestUnfinishedTask> getTasks() {
		return tasks;
	}

	public void setTasks(List<RequestUnfinishedTask> tasks) {
		this.tasks = tasks;
	}

	public LazyDataModel<RequestUnfinishedTask> getModel() {
		return model;
	}

	public void setModel(LazyDataModel<RequestUnfinishedTask> model) {
		this.model = model;
	}

	public RequestUnfinishedTask getSelected() {
		return selected;
	}

	public void setSelected(RequestUnfinishedTask selected) {
		this.selected = selected;
	}

	public Date getQueryDate1() {
		return queryDate1;
	}

	public void setQueryDate1(Date queryDate1) {
		this.queryDate1 = queryDate1;
	}

	public Date getQueryDate2() {
		return queryDate2;
	}

	public void setQueryDate2(Date queryDate2) {
		this.queryDate2 = queryDate2;
	}

	public int getSort() {
		return sort;
	}

	public void setSort(int sort) {
		this.sort = sort;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getRecordSize() {
		return recordSize;
	}

	public void setRecordSize(int recordSize) {
		this.recordSize = recordSize;
	}

}
